# - Try to find libunwind library and headers
# Once done, this will define
#
#  LIB_UNWIND_FOUND - system has libunwind
#  LIB_UNWIND_INCLUDE_DIR - the libunwind include directories
#  LIB_UNWIND_LIBRARIES - link these to use libunwind

if (LIB_UNWIND_INCLUDE_DIR AND LIB_UNWIND_LIBRARIES)
	set(LIB_UNWIND_FIND_QUIETLY TRUE)
endif ()

if (LIB_UNWIND_INSTALL_PATH)
	find_path(LIB_UNWIND_INCLUDE_DIR
			NAMES libunwind.h
			PATHS ${LIB_UNWIND_INSTALL_PATH}/include
			NO_DEFAULT_PATH)
	if (NOT CYGWIN)
		find_library(LIB_UNWIND_LIBRARIE_UNWIND
				NAMES unwind
				PATHS ${LIB_UNWIND_INSTALL_PATH}/lib
				NO_DEFAULT_PATH)
		if (NOT ${LIB_UNWIND_LIBRARIE_UNWIND} STREQUAL "NOTFOUND")
			set(LIB_UNWIND_LIBRARIES ${LIB_UNWIND_LIBRARIE_UNWIND})
		endif ()
	endif ()
else ()
	find_path(LIB_UNWIND_INCLUDE_DIR
			NAMES libunwind.h)
	if (NOT CYGWIN)
		find_library(LIB_UNWIND_LIBRARIE_UNWIND
				NAMES unwind)
		if (NOT ${LIB_UNWIND_LIBRARIE_UNWIND} STREQUAL "NOTFOUND")
			set(LIB_UNWIND_LIBRARIES ${LIB_UNWIND_LIBRARIE_UNWIND})
		endif ()
	endif ()
endif ()

include(FindPackageHandleStandardArgs)
if (NOT CYGWIN)
	find_package_handle_standard_args(LIB_UNWIND DEFAULT_MSG LIB_UNWIND_INCLUDE_DIR LIB_UNWIND_LIBRARIES)
else ()
	find_package_handle_standard_args(LIB_UNWIND DEFAULT_MSG LIB_UNWIND_INCLUDE_DIR)
endif ()

if (NOT LIB_UNWIND_FOUND)
	MESSAGE(SEND_ERROR "libunwind install path is invalid or cannot to be find, please install it or specify it by \"-DLIB_UNWIND_INSTALL_PATH=<value>\".")
endif ()

if (NOT CYGWIN)
	mark_as_advanced(LIB_UNWIND_INCLUDE_DIR LIB_UNWIND_LIBRARIES)
else ()
	mark_as_advanced(LIB_UNWIND_INCLUDE_DIR)
endif ()
